package com.hotel.controller;

import com.hotel.pojo.Hotel;
import com.hotel.pojo.Room;
import com.hotel.pojo.Staff;
import com.hotel.service.StaffService;
import com.hotel.vo.HotelVo;
import com.hotel.vo.RoomVo;
import com.hotel.vo.StaffVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/staff")
public class StaffController {

    @Autowired
    private StaffService staffService;

    //登录
    @PostMapping("login/{name}")
    public String staffLogin(@PathVariable("name") String name,
                             @RequestBody StaffVo staffVo) {
        Staff staff = new Staff();
        staff.setName(name);
        staff.setUsername(staffVo.getUsername());
        staff.setPassword(staffVo.getPassword());
        if(staffService.checkStaff(staff)) {
            if (staffService.isLogin(name)) {
                return "请勿重复登录！";
            } else {
                staffService.loginStaff(name);
                return "酒店员工登陆成功！";
            }
        } else {
            return "账号或者密码错误！";
        }
    }

    // 注销
    @PostMapping("exit/{name}")
    public String userExit(@PathVariable("name") String name) {
        if (staffService.findStaffByName(name)) {
            staffService.exitStaff(name);
            return "注销成功！";
        } else {
            return "工作人员不存在！";
        }
    }

//    // 查看所有员工信息
//    @GetMapping("{staffname}/findall")
//    public String findAllStaff(@PathVariable("staffname") String name) {
//        if(!staffService.isLogin(name)) {
//            return "请先登录！";
//        }
//        if (staffService.findStaffByName(name)) {
//            return staffService.findAllStaff().toString();
//        } else {
//            return "员工不存在，无查看权限！";
//        }
//    }

    // 更新信息
    @PostMapping("{staffname}/update")
    public String updateStaff(@PathVariable("staffname") String name,
                              @RequestBody StaffVo staffVo) {
        if(!staffService.isLogin(name)) {
            return "请先登录！";
        }
        if (staffService.findStaffByName(name)) {
            Staff staff = new Staff();
            staff.setName(name);
            staff.setUsername(staffVo.getUsername());
            staff.setPassword(staffVo.getPassword());
            staffService.updateStaff(staff);
            return "修改酒店员工信息成功";
        } else {
            return "修改酒店员工信息失败！";
        }
    }

    // 添加酒店
    @PostMapping("{staffname}/addhotel")
    public String addHotel(@PathVariable("staffname") String name,
                           @RequestBody HotelVo hotelVo) {
        if(!staffService.isLogin(name)) {
            return "请先登录！";
        }
        if (staffService.findStaffByName(name)) {
            Hotel hotel = new Hotel();
            hotel.setName(hotelVo.getHotelName());
            hotel.setAddress(hotelVo.getAddress());
            hotel.setPhone(hotelVo.getPhone());
            hotel.setIntroduction(hotelVo.getIntroduction());
            hotel.setStaff(hotelVo.getStaff());
            boolean b = staffService.addHotel(hotel);
            if (b) {
                return "添加酒店成功！";
            } else {
                return "酒店已存在，添加失败！";
            }
        } else {
            return "员工不存在，添加酒店失败";
        }
    }

    // 修改酒店信息
    @PostMapping("{staffname}/updatehotel")
    public String updateHotel(@PathVariable("staffname") String name,
                              @RequestBody HotelVo hotelVo) {
        if(!staffService.isLogin(name)) {
            return "请先登录！";
        }
        if (staffService.findStaffByName(name)) {
            String hotelStaff = staffService.findStaffByHotel(hotelVo.getHotelName());
            if (name.equals(hotelStaff)) {
                Hotel hotel = new Hotel();
                hotel.setName(hotelVo.getHotelName());
                hotel.setAddress(hotelVo.getAddress());
                hotel.setPhone(hotelVo.getPhone());
                hotel.setIntroduction(hotelVo.getIntroduction());
                // 可以修改酒店负责人为其他工作人员
                hotel.setStaff(hotelVo.getStaff());
                staffService.updateHotel(hotel);
                return "修改酒店信息成功！";
            } else {
                return "非本酒店负责人，无权修改！";
            }
        } else {
            return "员工不存在，修改酒店信息失败";
        }
    }

    // 查看负责的所有酒店
    @GetMapping("{staffname}/findallhotel")
    public String findAllHotel(@PathVariable("staffname") String name) {
        if(!staffService.isLogin(name)) {
            return "请先登录！";
        }
        if (staffService.findStaffByName(name)) {
            if(staffService.findHotelByStaff(name)!= null) {
                return staffService.findHotelByStaff(name).toString();
            } else {
                return "无此员工负责酒店信息！";
            }
        } else {
            return "员工不存在，查看酒店信息失败！";
        }
    }

    // 添加客房
    @PostMapping("{staffname}/addroom")
    public String addRoom(@PathVariable("staffname") String name,
                          @RequestBody RoomVo roomVo) {
        if(!staffService.isLogin(name)) {
            return "请先登录！";
        }
        if (staffService.findStaffByName(name)) {
            List<Hotel> hotelList = staffService.findHotelByStaff(name);
            boolean match = hotelList.stream().anyMatch(hotel -> hotel.getName().equals(roomVo.getHotelName()));
            if (match) {
                Room room = new Room();
                room.setHotelName(roomVo.getHotelName());
                room.setType(roomVo.getType());
                room.setPrice(roomVo.getPrice());
                room.setNum(roomVo.getNum());
                boolean b = staffService.addRoom(room);
                if (b) {
                    return "添加客房成功！";
                } else {
                    return "客房已存在，请勿重复添加！";
                }
            } else {
                return "无法添加该酒店客房，无权限！";
            }
        } else {
            return "员工不存在，添加客房失败";
        }
    }

    // 修改客房信息
    @PostMapping("{staffname}/updateroom")
    public String updateRoom(@PathVariable("staffname") String name,
                             @RequestBody RoomVo roomVo) {
        if(!staffService.isLogin(name)) {
            return "请先登录！";
        }
        if (staffService.findStaffByName(name)) {
            List<Hotel> hotelList = staffService.findHotelByStaff(name);
            boolean match = hotelList.stream().anyMatch(hotel -> hotel.getName().equals(roomVo.getHotelName()));
            if (match) {
                Room room = new Room();
                room.setHotelName(roomVo.getHotelName());
                room.setType(roomVo.getType());
                room.setPrice(roomVo.getPrice());
                room.setNum(roomVo.getNum());
                staffService.updateRoom(room);
                return "修改客房信息成功！";
            } else {
                return "无法修改该酒店客房，无权限！";
            }
        } else {
            return "员工不存在，修改客房信息失败";
        }
    }

//    // 查看所有客房
//    @GetMapping("{staffname}/findallroom")
//    public String findAllRoom(@PathVariable("staffname") String name) {
//        if(!staffService.isLogin(name)) {
//            return "请先登录！";
//        }
//        if (staffService.findStaffByName(name)) {
//            return staffService.findAllRoom().toString();
//        } else {
//            return "员工不存在，查看客房信息失败";
//        }
//    }
}